﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GtsServer.DAL;
using GtsServer.Service.Model;
using System.Data;

namespace GtsServer.Service.Controller
{
    public class UserController
    {
        UserDAL userDal = new UserDAL();

        public bool UpdateRequestTime(string addr)
        {
            return userDal.UpdateRequestTime(DateTime.Now, addr);
        }

        public UserLoginResponse UserLogin(UserRegisterRequest request)
        {
            UserLoginResponse response = new UserLoginResponse()
            {
                user = new UserLoginManage()
            };

            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;

            DataSet ds = userDal.LoginUser(userAccount, request.user.account, request.user.password);
            if (ds.Tables[0].Rows.Count > 0)
            {
                response.user.account = ds.Tables[0].Rows[0]["account"].ToString();
                response.user.password = ds.Tables[0].Rows[0]["password"].ToString();
            }

            return response;
        }

        public UserRegisterResponse UserRegister(UserRegisterRequest request)
        {
            UserRegisterResponse response = new UserRegisterResponse();
            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;

            DataSet ds = userDal.RegisterUser(userAccount, request.user.account, request.user.password);
            if (ds.Tables[0].Rows.Count > 0)
            {
                response.register = ds.Tables[0].Rows[0][0].ToString();
            }

            return response;
        }
    }
}
